/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_EPSCtrl.h
 *
 *  Created on: 2023年2月7日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_EPSCTRL_H_
#define PERIPHERAL_LAYER_INC_LOS_EPSCTRL_H_

#include "LOS_SYS.h"

#ifndef NOW_LAUNCHPAD
#define EPSCTRL_MAXNUM LOS_MAX14830_NUM2
#define EPSCTRL_MAXPort 1

#define TEST_MAXNUM LOS_MAX14830_NUM2
#define TEST_MAXPort 1
#else
#define EPSCTRL_MAXNUM LOS_MAX14830_NUM2
#define EPSCTRL_MAXPort 0
#endif

#define SWITCH_ON 0X4F
#define SWITCH_OFF 0X46

#define SWITCH_STO_PD  1
#define SWITCH_DTBB_PD 2
#define SWITCH_DTCH_PD 3
#define SWITCH_NVE_PD  4
#define SWITCH_SPT_PD  5
#define SWITCH_GNSS_PD 8
#define SWITCH_SUNS_PD 12
#define SWITCH_FWX_PD  13
#define SWITCH_FWY_PD  14
#define SWITCH_FWZ_PD  15
#define SWITCH_FWS_PD  16
#define SWITCH_CAM_PD  17
#define SWITCH_LEN1_PD 18
#define SWITCH_CMOS_PD 19
#define SWITCH_MEMS_PD 20
#define SWITCH_AMUR_PD 21
#define SWITCH_SAX1_PD 22
#define SWITCH_SAX2_PD 23
#define SWITCH_LEN2_PD 24

#define SWITCH_SSY_TC 13
#define SWITCH_SSZ_TC 14
#define SWITCH_MAG_TC 15
#define SWITCH_YH50_TC 16
#define SWITCH_UHF1_TC 19
#define SWITCH_UHF2_TC 20
#define SWITCH_VHF1_TC 21
#define SWITCH_VHF2_TC 22

typedef struct LOS_EPS_t
{
    uint8_t VAL;    //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;    //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;    //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG[2];            //通信状态，通信正常为0，通信异常为其他值.0:配电扩展板 1：热控扩展板
    uint8_t RBCNT[3];          //复位次数 0：配电扩展板 1：热控扩展板 3:电源控制器
    uint8_t HL_TXF_EPS;     //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_TXF_EX;
    uint8_t HL_TXF_TC;
    uint8_t HL_WCNT;        //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint32_t TMCNT_PD;         //遥测计数器
    uint32_t TMCNT_TC;         //遥测计数器
    uint8_t SWITCH_PD[24];  //配电扩展板一共24路开关 1
    uint8_t SWITCH_TC[24];  //热控扩展板一共24路开关 1
    uint8_t SWITCH_PD_O[3]; //配电扩展板开关状态原始数据
    uint8_t SWITCH_TC_O[3]; //热控扩展板开关状态原始数据
    uint16_t Cur_PD25940[24]; //配电扩展板TPS25940电流监测值，单位mA 1
    uint16_t Cur_TC25940[24]; //热控扩展板TPS25940电流监测值，单位mA 1
    uint8_t Cur_PDMSP430;     //配电扩展板MSP430电流 1
    uint8_t Cur_TCMSP430;     //热控扩展板MSP430电流 1
    uint8_t ball;           //离轨球展开状态，0x4F开 0x46关 0x55异常 1
    int8_t Temp_MPPT[3];    //MPPT1 2 3温度  1
    int8_t Temp_Charger[2]; //充电器1 2温度   1
    int8_t Temp_3V3;        //3.3V温度    1
    int8_t Temp_5V;         //5V温度      1
    int8_t Temp_12V;        //12V温度     1
    int16_t Vol_MPPT[3];    //MPPT电压    1
    int16_t Vol_Charger[2]; //充电器电压   1
    int16_t Vol_3V3;        //3.3V电压    1
    int16_t Vol_5V;         //5V电压      1
    int16_t Vol_12V;        //12V电压     1
    int16_t Cur_MPPT[3];    //MPPT电流    1
    int16_t Cur_Charger[2]; //充电器电流   1
    int16_t Cur_3V3;        //3.3V电流    1
    int16_t Cur_5V;         //5V电流      1
    int16_t Cur_12V;        //12V电流     1
    uint16_t Vol_SA[3];     //分阵1 2 3电压
    int8_t Temp_Ther[32];   //热敏电阻温度
    int8_t Heater_UP[14];   //加热带温度上限设置 
    int8_t Heater_LOW[14];  //加热带温度下限设置
    uint16_t Vcell[3];      //电池电压
    uint16_t PD_CMDCNT;     //配电扩展板指令计数
    uint16_t TC_CMDCNT;     //热控扩展板指令计数
    uint16_t PD_LASTCMD;    //配电-最后一条指令
    uint16_t TC_LASTCMD;    //热控-最后一条指令
    uint16_t PD_CMDERRCNT;  //配电-错误指令计数
    uint16_t TC_CMDERRCNT;  //热控-错误指令计数
    uint8_t PD_CRCERRCNT;   //配电-CRC错误计数
    uint8_t TC_CRCERRCNT;   //热控-CRC错误计数
    uint8_t PD_RSTFLAG;     //配电重启标志
    uint8_t TC_RSTFLAG;     //热控重启标志
    float AhMeter;          //安全模式充电安时计
    float Mother_Vol;       //母线电压（光照时为MPPT1电压，无光照时为充电器1电压）
    uint32_t PD_LastTime;
    uint32_t TC_LastTime;
}LOS_EPS_t;

typedef enum EPS_ENUM
{
    EPS_TMOK = 0,
    EPS_WrongHead = 1,
    EPS_PDError = 2,
    EPS_EPSError = 3,
    EPS_TCError = 4,
    EPS_TempError = 5
}EPS_ENUM;

extern uint8_t PDSW_NUM;
extern uint8_t PDSW_STA;

extern uint8_t TCSW_NUM;
extern uint8_t TCSW_STA;

extern uint8_t EPS_RCV_FLAG;
extern LOS_EPS_t STA_EPS;

extern uint8_t TMPACK_PDB[138];
extern uint8_t TMPACK_TCB[138];

extern uint16_t PD_TCCNT;
extern uint16_t TC_TCCNT;

void LOS_EPS_GetEXSTA();
void LOS_EPS_Switch(uint8_t payload, uint8_t sta);
void LOS_EPS_ResetPD();
void LOS_EPS_ResetEPS();
void LOS_EPS_GetTCSTA();
void LOS_EPS_GetSTA();
void LOS_EPS_TCSwitch(uint8_t payload, uint8_t sta);
void LOS_EPS_TurnOffAll();
void LOS_EPS_ResetTC();
void LOS_EPS_TCSetTemp(uint8_t position, uint8_t tempup, uint8_t templow);
void LOS_EPS_ResetPDH();
void LOS_EPS_ResetTCH();
void LOS_EPS_RecoverPDH();
void LOS_EPS_RecoverTCH();
uint8_t LOS_EPS_TWOTHREE(uint8_t cp0, uint8_t cp1, uint8_t cp2);
EPS_ENUM LOS_EPS_DataProcess(uint8_t* data);
void LOS_EPS_PDTKSW(uint8_t payload, uint32_t dur);
void LOS_EPS_TCTKSW(uint8_t payload, uint32_t dur);


#endif /* PERIPHERAL_LAYER_INC_LOS_EPSCTRL_H_ */
